from Simulinux.Views.View import View

__all__ = ['PinView', 'PIN_LENGTH', 'PIN_HORIZONTAL', 'PIN_VERTICAL']

TOLERANCE_Y = 3
TOLERANCE_X = 1
PIN_LENGTH = 5
PIN_HORIZONTAL = 0
PIN_VERTICAL = 1

class PinView(View):
    def __init__(self, pin, direction, x1, y1, x2, y2):
        self.pin = pin
        self.direction = direction
        if x2 == x1 and y2 == y1:
            raise RuntimeError("Invalid pin coordinates.")
        if x2 > x1:
            self.x1 = x1
            self.x2 = x2
        else:
            self.x1 = x2
            self.x2 = x1
        if y2 > y1:
            self.y1 = y1
            self.y2 = y2
        else:
            self.y1 = y2
            self.y2 = y1
    def is_available(self):
        return not self.pin.has_net()
    def get_coords(self):
        return (self.pin.get_block().posx + self.x2, self.pin.get_block().posy + self.y2)
    def is_my_region(self, x, y):
        xr = x - self.pin.get_block().posx
        yr = y - self.pin.get_block().posy
        if xr > (self.x1 - TOLERANCE_X) and \
           xr < (self.x2 + TOLERANCE_X) and \
           yr > (self.y1 - TOLERANCE_Y) and \
           yr < (self.y2 + TOLERANCE_Y):
            return True
        else:
            return False
    def draw(self, context, blockx, blocky):
        context.move_to(blockx, blocky)
        context.rel_move_to(self.x1, self.y1)
        context.rel_line_to(self.x2 - self.x1, self.y2 - self.y1)
    def edit_properties(self):
        pass
